package cn.elwy.rcp.ui.handler;

import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

/**
 * 显示Action的文字和图标
 * 
 * @author huangsq
 * @version 1.0, 2013-11-20
 * @since 1.0, 2013-11-20
 */
public class ShowToolBarText extends ContributionItem {

  public void fill(ToolBar parent, int index) {
    ToolItem[] toolItems = parent.getItems();
    for (ToolItem toolItem : toolItems) {
      if (toolItem.getData() instanceof ActionContributionItem) {
        ActionContributionItem contributionItem = (ActionContributionItem) toolItem.getData();
        IAction action = contributionItem.getAction();
        if (toolItem.getText() == null || toolItem.getText().length() == 0) {
          toolItem.setText(action.getText());
        }
      }
    }
  }

  /**
   * 调用方式
   * 
   * @return the toolbar manager
   */
  protected ToolBarManager createToolBarManager(int style) {
    ToolBarManager toolBarManager = new ToolBarManager(style);
    toolBarManager.add(new Separator());
    // toolBarManager.add(exitAction);
    // 在所有action添加后，添加以下方法
    toolBarManager.add(new ShowToolBarText());
    return toolBarManager;
  }

  /**
   * 调用方式
   * 
   * @param coolBar
   */
  protected void fillCoolBar(ICoolBarManager coolBar) {
    ToolBarManager menu = new ToolBarManager(SWT.FLAT | SWT.WRAP);
    coolBar.add(menu);
    menu.add(new ShowToolBarText());
  }

}
